#!/usr/bin/env bash
###################################################################
# Function :CentOS7.X 安装mysql                                    #
# Platform :RedHatEL7.x Based Platform                            #
# Version  :1.0                                                   #
# Date     :2020-08-28                                            #
# Author   :houxiaoshuai@baidu.com & hxsaj@126.com                #
###################################################################
function MysqlDownloadSource() # 下载 mysql 源代码
{
#!/usr/bin/env bash
#  打印下资源
echo "
1，中科大  http://mirrors.ustc.edu.cn/mysql-ftp/downloads
2，华为云  https://mirrors.huaweicloud.com/mysql/Downloads
3，北外源  https://mirrors.bfsu.edu.cn/mysql/downloads
4，北理源  http://mirror.bit.edu.cn/mysql/downloads
5，南大源  http://mirrors.nju.edu.cn/mysql/downloads
6，清华源  https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads

q，退出
"

#  请选择下载的源
read -p "请选择 mysql 源代码下载源(1,2,3......or q)：" Choose_Download_mysql

#  判断下载源
[ ${Choose_Download_mysql}t == "1t" ] && Download_Addr="http://mirrors.ustc.edu.cn/mysql-ftp/Downloads"
[ ${Choose_Download_mysql}t == "2t" ] && Download_Addr="https://mirrors.huaweicloud.com/mysql/Downloads"
[ ${Choose_Download_mysql}t == "3t" ] && Download_Addr="https://mirrors.bfsu.edu.cn/mysql/downloads"
[ ${Choose_Download_mysql}t == "4t" ] && Download_Addr="http://mirror.bit.edu.cn/mysql/downloads"
[ ${Choose_Download_mysql}t == "5t" ] && Download_Addr="http://mirrors.nju.edu.cn/mysql/downloads"
[ ${Choose_Download_mysql}t == "6t" ] && Download_Addr="https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads"
#  输入其他字符，则退出
if echo ${Choose_Download_mysql}|egrep -v "[1-9]" >>/dev/null 2>&1;then exit 0;fi

#  判断地址，检索mysql的版本
if echo ${Choose_Download_mysql}|egrep "1|5" > /dev/null 2>&1;then
	for i in $(curl -s ${Download_Addr}/|grep MySQL-[0-9]|awk -F "\"" '{print$2}'|awk -F "/" '{print$1}');
	do
		curl -s ${Download_Addr}/${i}/|awk -F "\"" '{print$2}'|egrep mysql-[0-9].[[0-9].[0-9]\{1,\}.tar.gz.md5|awk -F ".t" '{print$1}'
	done
else
	for i in $(curl -s ${Download_Addr}/|grep MySQL-[0-9]|awk -F "\"" '{print$6}'|awk -F "/" '{print$1}');
	do
		curl -s ${Download_Addr}/${i}/|awk -F "\"" '{print$6}'|egrep mysql-[0-9].[[0-9].[0-9]\{1,\}.tar.gz.md5|awk -F ".t" '{print$1}'
	done
fi

#  输入需要下载的版本
echo
read -p "复制以上其中一个版本下载：" Mysql_tarball
#  获取主版本号
Major_Version_Number=$(echo ${Mysql_tarball}|awk -F "-" '{print$2}'|awk -F "." '{print$1"."$2}')
#  下载文件（此函数的核心目标）
echo -e "\n  开始下载......\n"
wget -c ${Download_Addr}/MySQL-${Major_Version_Number}/${Mysql_tarball}.tar.gz
}


